package com.easykoo.web.tag;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import java.io.IOException;

public class PagerTag extends BaseTag {
    private String styleClass;
    private String style;
    private String background;
    private String width;
    private String height;
    private String id;
    private String bgcolor;
    private String font;
    private String type;

    private String firstPage;
    private String prePage;
    private String nextPage;
    private String lastPage;
    private String goTo;

    private JspWriter out;

    public int doStartTag() throws JspException {

        FormTag formTag = (FormTag) getParent();
        String items = formTag.getItems();
        String url = formTag.getUrl();
        String imagePath = formTag.getPageInfo().getImagePath();
        String imageColor = formTag.getPageInfo().getImageColor();
        PageInfo pageInfo = (PageInfo) pageContext.getSession().getAttribute(items);

        if (pageInfo != null && pageInfo.getResult() != null && pageInfo.getResult().size() != 0) {
            StringBuffer body = new StringBuffer("");
            if (pageInfo.hasPrevious()) {
                if (PageInfo.TYPE_IMG.equals(type)) {
                    //left end
                    body.append("<a ");
                    body.append("name=\"" + pageInfo.getPrePage() + "\" href=\"javascript:window.open(\'" + url + "?page=1\', \'_self\')\">");
                    body.append("<img src=\"" + imagePath + imageColor + "_left_end.gif" + "\" />");
                    body.append("</a>");
                    body.append("&nbsp;");
                    //left
                    body.append("<a ");
                    body.append("name=\'" + pageInfo.getPrePage() + "\' href=\"javascript:window.open(\'" + url + "?page=" + pageInfo.getPrePage() + "\', \'_self\')\">");
                    body.append("<img src=\"" + imagePath + imageColor + "_left.gif" + "\" />");
                    body.append("</a>");
                } else {
                    //left end
                    body.append("<a ");
                    body.append("name=\"" + pageInfo.getPrePage() + "\" href=\"javascript:window.open(\'" + url + "?page=1\', \'_self\')\">");
                    body.append(this.firstPage);
                    body.append("</a>");
                    body.append("&nbsp;");
                    //left
                    body.append("<a ");
                    body.append("name=\'" + pageInfo.getPrePage() + "\' href=\"javascript:window.open(\'" + url + "?page=" + pageInfo.getPrePage() + "\', \'_self\')\">");
                    body.append(this.prePage);
                    body.append("</a>");
                }
            } else {
                if (PageInfo.TYPE_IMG.equals(type)) {
                    body.append("<img src=\"" + imagePath + imageColor + "_left_end_faint.gif" + "\" />");
                    body.append("&nbsp;");
                    body.append("<img src=\"" + imagePath + imageColor + "_left_faint.gif" + "\" />");
                } else {
                    body.append(this.getFirstPage());
                    body.append("&nbsp;");
                    body.append(this.prePage);
                }
            }
            body.append("&nbsp;");
            body.append(pageInfo.getCurrPage());
            body.append("/");
            body.append(pageInfo.getTotalPage());
            body.append("&nbsp;");
            if (pageInfo.hasNext()) {
                if (PageInfo.TYPE_IMG.equals(type)) {
                    body.append("<a ");
                    body.append(" name=\'" + pageInfo.getNextPage() + "\' href=\"javascript:window.open(\'" + url + "?page=" + pageInfo.getNextPage() + "\', \'_self\')\">");
                    body.append("<img src=\"" + imagePath + imageColor + "_right.gif" + "\" />");
                    body.append("</a>");
                    body.append("&nbsp;");
                    body.append("<a ");
                    body.append(" name=\'" + pageInfo.getNextPage() + "\' href=\"javascript:window.open(\'" + url + "?page=" + pageInfo.getTotalPage() + "\', \'_self\')\">");
                    body.append("<img src=\"" + imagePath + imageColor + "_right_end.gif" + "\" />");
                    body.append("</a>");
                } else {
                    body.append("<a ");
                    body.append(" name=\'" + pageInfo.getNextPage() + "\' href=\"javascript:window.open(\'" + url + "?page=" + pageInfo.getNextPage() + "\', \'_self\')\">");
                    body.append(this.nextPage);
                    body.append("</a>");
                    body.append("&nbsp;");
                    body.append("<a ");
                    body.append(" name=\'" + pageInfo.getNextPage() + "\' href=\"javascript:window.open(\'" + url + "?page=" + pageInfo.getTotalPage() + "\', \'_self\')\">");
                    body.append(this.lastPage);
                    body.append("</a>");
                }
            } else {
                if (PageInfo.TYPE_IMG.equals(type)) {
                    body.append("<img src=\"" + imagePath + imageColor + "_right_faint.gif" + "\" />");
                    body.append("&nbsp;");
                    body.append("<img src=\"" + imagePath + imageColor + "_right_end_faint.gif" + "\" />");
                } else {
                    body.append(this.nextPage);
                    body.append("&nbsp;");
                    body.append(this.lastPage);
                }
            }

            body.append("&nbsp;&nbsp;&nbsp;&nbsp;");
            body.append("<a ");
            body.append("name=\'" + formTag.getItems() + "_toPage\' href=\"javascript:gotoPage(\'" + formTag.getItems() + "_toPage\')\">" + this.goTo + "</a>");
            body.append("&nbsp;");
            body.append("<input");
            appendAttr(body);
            body.append("type=\"text\" maxlength=\"2\" size=\"2\" id=\"" + formTag.getItems() + "_toPage\" />");
            body.append("<script type=\"text/javascript\">");
            body.append("function gotoPage(id){");
            body.append("var reg = /^[-+]?\\d*$/;");
            body.append("if(reg.test(document.getElementById(id).value)){");
            body.append("window.open(\'" + url + "?page=\'+document.getElementById(id).value, \'_self\')");
            body.append("}}");
            body.append("</script>");

            out = this.pageContext.getOut();
            try {
                out.write(body.toString());
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    out.flush();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return SKIP_BODY;
    }

    public String getStyleClass() {
        return styleClass;
    }

    public void setStyleClass(String styleClass) {
        this.styleClass = styleClass;
    }

    public String getStyle() {
        return style;
    }

    public void setStyle(String style) {
        this.style = style;
    }

    public String getBackground() {
        return background;
    }

    public void setBackground(String background) {
        if (background.contains("/")) {
            if (background.startsWith("/")) {
                this.background = ((HttpServletRequest) pageContext.getRequest()).getContextPath() + background;
            } else {
                this.background = ((HttpServletRequest) pageContext.getRequest()).getContextPath() + "/" + background;
            }
        } else {
            this.background = background;
        }
    }

    public String getWidth() {
        return width;
    }

    public void setWidth(String width) {
        this.width = width;
    }

    public String getHeight() {
        return height;
    }

    public void setHeight(String height) {
        this.height = height;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getBgcolor() {
        return bgcolor;
    }

    public void setBgcolor(String bgcolor) {
        this.bgcolor = bgcolor;
    }

    public String getFont() {
        return font;
    }

    public void setFont(String font) {
        this.font = font;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getFirstPage() {
        return firstPage;
    }

    public void setFirstPage(String firstPage) {
        if (isNotBlank(firstPage)) {
            this.firstPage = firstPage;
        } else {
            this.firstPage = PageInfo.FIRST_PAGE;
        }
    }

    public String getPrePage() {
        return prePage;
    }

    public void setPrePage(String prePage) {
        if (isNotBlank(prePage)) {
            this.prePage = prePage;
        } else {
            this.prePage = PageInfo.FIRST_PAGE;
        }
    }

    public String getNextPage() {
        return nextPage;
    }

    public void setNextPage(String nextPage) {
        if (isNotBlank(nextPage)) {
            this.nextPage = nextPage;
        } else {
            this.nextPage = PageInfo.NEXT_PAGE;
        }
    }

    public String getLastPage() {
        return lastPage;
    }

    public void setLastPage(String lastPage) {
        if (isNotBlank(lastPage)) {
            this.lastPage = lastPage;
        } else {
            this.lastPage = PageInfo.LAST_PAGE;
        }
    }

    public String getGoTo() {
        return goTo;
    }

    public void setGoTo(String goTo) {
        if (isNotBlank(goTo)) {
            this.goTo = goTo;
        } else {
            this.goTo = PageInfo.GO_TO;
        }
    }
}